<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('审核')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />

</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-product-edit" th:object="${product}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">商品名称：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">商品描述：</label>
                <div class="col-sm-8">
                    <input name="des" th:field="*{des}" class="form-control" type="text" readonly>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">商品简介：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{content}">
                    <div class="summernote" id="content"></div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">销量：</label>
                <div class="col-sm-8">
                    <input name="count" th:field="*{count}" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">库存：</label>
                <div class="col-sm-8">
                    <input name="inventory" th:field="*{inventory}" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">商品图片：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="imglist" th:field="*{imglist}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="imglist" name="file" type="file">
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">商品地区：</label>
                <div class="col-sm-8">
                    <input name="prov" th:field="*{prov}" class="form-control" type="text" readonly>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">商品价格：</label>
                <div class="col-sm-8">
                    <input name="price" th:field="*{price}" class="form-control" type="text" readonly>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">商品状态：</label>
                <div class="col-sm-8">
                    <select name="status" class="form-control m-b" th:with="type=${@dict.getType('product_status')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{status}"></option>
                    </select>
                </div>
            </div>
        </form>

    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js" />

    <script th:inline="javascript">
        var prefix = ctx + "platform/product";
        $("#form-product-edit").validate({
            focusCleanup: true
        });

        $(document).ready(function() {
            $('#content').summernote('editor.insertText', 'hello world');

        });
        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-product-edit').serialize());
            }
        }

        var List = new Array();
        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            var valarr = val.split(",")
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: valarr,
                maxFileCount: 5,
                autoReplace: true,
                showRemove: false,
                showUpload:false,
                showCancel:false,
                showBrowse: false

            }).on('fileuploaded', function (event, data, previewId, index) {
                List.push({ FileName: data.response.url, KeyID: previewId })
                const fileNamesArray = List.map(item => item.FileName);

                const resultString = fileNamesArray.join(',');
                $("input[name='" + event.currentTarget.id + "']").val(resultString)
                var url1 = $("input[name='" + event.currentTarget.id + "']").val()
                console.log(url1)            }).on('fileremoved', function (event, id, index) {
                for (var i = 0; i < List.length; i++) {
                    if (List[i].KeyID == data) {
                        List.splice(i,1)
                    }
                }
                const fileNamesArray = List.map(item => item.FileName);

                const resultString = fileNamesArray.join(',');
                $("input[name='" + event.currentTarget.id + "']").val(resultString)
                var url1 = $("input[name='" + event.currentTarget.id + "']").val()
                console.log(url1)                })
            $(this).fileinput('_initFileActions');
        });
    </script>
    <script>

        // 前端JS代码
        $(document).ready(function() {
            // 页面加载完成时渲染省份选项
            renderProvinceOptions();

            // 当省份选择框的值发生改变时
            $('#province').change(function() {
                var provinceId = $(this).val(); // 获取选中的省份ID
                if (provinceId != 0) {
                    // 发送Ajax请求获取对应省份的市区列表
                    $.ajax({
                        url: '/city/getCityListByid',
                        type: 'GET',
                        data: { id: provinceId },
                        success: function(data) {
                            console.log(data)
                            // 清空市区选择框的选项
                            $('#city').empty();
                            // 添加新的市区选项
                            $('#city').append('<option value="-1">--请选择市区--</option>');
                            // 将返回的市区数据添加到市区选择框中
                            $.each(data.data, function(index, city) {
                                $('#city').append('<option value="' + city.districtId + '">' + city.districtName + '</option>');
                            });
                        },
                        error: function(xhr, status, error) {
                            console.error('获取市区列表失败: ' + error);
                        }
                    });
                } else {
                    // 如果省份选择框选择的是“--请选择省份--”，则清空市区选择框
                    $('#city').empty();
                    $('#city').append('<option value="0">--请选择市区--</option>');
                }
            });
        });

        // 渲染省份选项的函数
        function renderProvinceOptions() {
            // 发送Ajax请求获取省份列表
            $.ajax({
                url: '/city/getProvinceList',
                type: 'GET',
                success: function(data) {
                    // 清空省份选择框的选项
                    $('#province').empty();
                    // 添加新的省份选项
                    console.log(data)
                    $('#province').append('<option value="-1">--请选择省份--</option>');
                    // 将返回的省份数据添加到省份选择框中
                    $.each(data.data, function(index, province) {
                        $('#province').append('<option value="' + province.districtId + '">' + province.districtName + '</option>');
                    });
                },
                error: function(xhr, status, error) {
                    console.error('获取省份列表失败: ' + error);
                }
            });
        }
        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    dialogsInBody: true,
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });


    </script>
</body>
</html>